const user = {
    logged: $.cookie('tel') && $.cookie('token'),
    tel: $.cookie('tel'),

    register: function (tel, password, code, callback) {
        $.ajax({
            type: 'POST',
            url: '/user/register',
            data: {
                tel: tel,
                password: password,
                code: code
            },
            success: function (json) {
                user.tel = tel;
                user.logged = true;
                callback(json.msg, true);
            },
            error: function (o) {
                callback(getAjaxErrorMsg(o), false);
            }
        });
    },

    login: function (tel, password, callback) {
        $.ajax({
            type: 'POST',
            url: '/user/login',
            data: {
                tel: tel,
                password: password
            },
            success: function (json) {
                user.tel = tel;
                user.logged = true;
                callback(json.msg, true);
            },
            error: function (o) {
                callback(getAjaxErrorMsg(o), false);
            }
        });
    },

    logout: function (callback) {
        this.logged = false;
        $.cookie('token', '');
        callback();
    },

    resetpsw: function (tel, newpsw, code, callback) {
        $.ajax({
            type: 'POST',
            url: '/user/resetpsw',
            data: {
                tel: tel,
                newpsw: newpsw,
                code: code
            },
            success: function (json) {
                user.tel = tel;
                user.logged = true;
                callback(json.msg, true);
            },
            error: function (o) {
                callback(getAjaxErrorMsg(o), false);
            }
        });
    }
};

